const fs = require('fs')
const program = require('commander')
const download = require('download-git-repo')
const handlebars = require('handlebars')
const inquirer = require('inquirer')
const ora = require('ora')
const chalk = require('chalk')
const symbols = require('log-symbols')

const GiteeUrl = 'https://gitee.com:redbike/vue-template#master'
program.version('1.0.0', '-v, --version')
    .command('init <name>')
    .action((name) => {
        if (!fs.existsSync(name)) {
            const prompt = [
                {
                    name: 'description',
                    message: '请输入项目描述'
                },
                {
                    name: 'author',
                    message: '请输入作者名称'
                }
            ]

            inquirer.prompt(prompt).then(({description, author}) => {
                const spinner = ora('正在下载模板...')
                spinner.start()
                download(GiteeUrl, name, {clone: true}, err => {
                    if (err) {
                        spinner.fail()
                        console.log(symbols.error, chalk.red(err))
                    } else {
                        spinner.succeed()
                        const fileName = `${name}/package.json`
                        const meta = {
                            name,
                            description,
                            author
                        }
                        if (fs.existsSync(fileName)) {
                            const content = fs.readFileSync(fileName).toString()
                            const result = handlebars.compile(content)(meta)
                            fs.writeFileSync(fileName, result)
                        }
                        console.log(symbols.success, chalk.green('项目初始化完成'))
                    }
                })
            })
        } else {
            // 错误提示项目已存在，避免覆盖原有项目
            console.log(symbols.error, chalk.red('项目已存在'))
        }
    })
program.parse(process.argv)
